/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.utils;

import org.testis.nicmodule.core.NicModuleException;
import org.testis.nicmodule.core.NicModuleExceptionType;
import java.util.ArrayList;
import java.util.List;


/**
 *main aim is to parse the output and get the result in an appropriate way
 * can be further improved for a genreic putğu parser but for the sake of time usage route -n is implemented
 * @author alperb
 */
public class GwRecord {

    String[] m_destination;
    String[] m_gateway;
    String[] m_genMask;
    String[] m_iFace;


    GwRecord(int i) {
        this.m_destination = new String[i]; // route -n output's 4 fields
        this.m_gateway = new String[i];
        this.m_genMask = new String[i];
        this.m_iFace = new String[i];
    }


    public String getDestination(int i) {
        return m_destination[i];
    }


    public void setDestination(String m_destination,
                                int i) {
        this.m_destination[i] = m_destination;
    }


    public String getGateway(int i) {
        return m_gateway[i];
    }


    public void setGateway(String m_gateway,
                            int i) {

        this.m_gateway[i] = m_gateway;
    }


    public String getGenMask(int i) {
        return m_genMask[i];
    }


    public void setGenMask(String m_genMask,
                            int i) {
        this.m_genMask[i] = m_genMask;
    }


    public String getIFace(int i) {
        return m_iFace[i];
    }


    public void setIFace(String m_iFace,
                          int i) {
        this.m_iFace[i] = m_iFace;
    }

/**
 * @param String to be parsed according to route -n
 * @return a GwRecord instance
 **/
    public static GwRecord setInstance(String inStr) {

        try {
            GwRecord tmpInstance = null;
            String[] line = inStr.split("\n");
            tmpInstance = new GwRecord(line.length);
            for (int i = 0; i < line.length; i++) {

                String[] subColumn = line[i].split("\\s+");
                tmpInstance.setDestination(subColumn[0],
                                           i);
                tmpInstance.setGateway(subColumn[1],
                                       i);
                tmpInstance.setGenMask(subColumn[2],
                                       i);
                tmpInstance.setIFace(subColumn[7],
                                     i);

            }

            return tmpInstance;
        } catch (Exception ex) {
            return null;
            
        }
    }

/**
 * @param String for interface name
 * @return array of string comprised of interfaces gateway
 **/
    public String[] findGateways(String iFace) {

        try {
            List<String> gwStr = new ArrayList<String>();
            int cnt = 0;
            if (this.equals(null)) {
                return null;
            }

            for (int i = 0; i < this.m_gateway.length; i++) {
                if (this.getIFace(i).equals(iFace)) {
                    gwStr.add(this.getGateway(i));
                    cnt++;
                }
            }

            String retStr[] = gwStr.toArray(new String[cnt]);
            return retStr;
        } catch (Exception ex) {
            return null;
        }
    }
}
